/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.edmo.fastener.parser;

import com.edmo.fastener.business.ListofCustomersnSuppliers;
import com.edmo.fastener.dao.CustomerOrderDao;
import com.edmo.fastener.domain.Customer;
import com.edmo.fastener.domain.CustomerOrder;
import com.edmo.fastener.slave.IocBean;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 *
 * @author Alexx
 */
public class ParseCustomer extends AbstractParser {
    
    private ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
    
    public void readnParse(){
        IocBean ioc = (IocBean) context.getBean("customer");
        readFile(ioc.getName());
        setOrderId();
    }
    
    private void setOrderId(){
        ApplicationContext context = new ClassPathXmlApplicationContext(new String[]{"aplication-context.xml"});
        CustomerOrderDao customerOrderDao = (CustomerOrderDao) context.getBean("customerOrderDao");
        try {
            List<CustomerOrder> list = customerOrderDao.getOrders();
            ListofCustomersnSuppliers.setOrderId(Long.valueOf(list.size()));
        } catch (Exception e){
            ListofCustomersnSuppliers.setOrderId(Long.valueOf(0));
        }
    }

    @Override
    public void parse(String s) {
        String[] arr = s.split("\\|");
        Customer list = new Customer();
        list.setCustomerName(arr[0]);
        list.setState(arr[1]);
        ListofCustomersnSuppliers.getInstance().getCustomers().add(list);
    }
    
}
